unit uFrmWait;

interface

uses
  SysUtils, Classes, Windows, Messages, Variants, Graphics, Controls,
  Forms, Dialogs, GIFImg, ExtCtrls, StdCtrls, uCxtForm, Buttons;

type
  PWaitSetting = ^TWaitSetting;
  TWaitSetting = record
    Next: PWaitSetting;
    Hint: string; 
  end;
  
  TFrmWait = class(TBaseForm)
    imgWait: TImage;
    lblMessage: TLabel;
    btnExitProcess: TSpeedButton;
    Label1: TLabel;
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btnExitProcessClick(Sender: TObject);
  private
    NestedWait: PWaitSetting;
  public
    procedure BeginWait(const msg: string);
    procedure EndWait;
  end;

procedure ShowWaitForm(const msg: string);
procedure HideWaitForm;

var
  FrmWait: TFrmWait;
  
implementation
	
procedure ShowWaitForm(const msg: string);
begin
  FrmWait.BeginWait(msg);
end;

procedure HideWaitForm;
begin
  FrmWait.EndWait;
end;

{$r *.dfm}

procedure TFrmWait.BeginWait(const msg: string);
var
  nw: PWaitSetting;
begin
  if not Assigned(NestedWait) then
  begin
    New(NestedWait);
    NestedWait^.Hint := msg;
    NestedWait.Next := nil;
    FrmWait.lblMessage.Caption := msg;
    FrmWait.ShowModal;
  end
  else begin
    New(nw);
    nw.Next := NestedWait;
    nw.Hint := msg;
    NestedWait := nw;
    FrmWait.lblMessage.Caption := msg;
  end;
end;

procedure TFrmWait.btnExitProcessClick(Sender: TObject);
begin
  Application.Terminate;
end;

procedure TFrmWait.EndWait;
var
  nw: PWaitSetting;
begin
  if NestedWait = nil then Exit;
  nw := NestedWait;
  NestedWait := nw.Next;
  Dispose(nw);
  if NestedWait = nil then
  begin
    FrmWait.ModalResult := mrOk;
    FrmWait.Hide;
  end
  else FrmWait.lblMessage.Caption := NestedWait.Hint;
end;

procedure TFrmWait.FormCreate(Sender: TObject);
begin
  Self.DoubleBuffered := True;
end;

procedure TFrmWait.FormShow(Sender: TObject);
begin
  with TGIFImage(imgWait.Picture.Graphic) do
  begin
    AnimationSpeed := 500;
    Animate := True;
  end;
end;

end.
